package controller;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pagemodel.FilmGrid;
import pagemodel.MSG;
import pagemodel.filmContextGrid;
import po.Film;
import po.filmContext;
import service.FilmService;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;


/*
    后端已实现： 查询所有电影、根据条件查询电影（condition，keyword）、删除一部电影、添加一部电影
                获取指定电影的影评信息
*   前端未实现的功能有：
*
* */
@Controller
public class FilmController {
    @Autowired
    FilmService filmService;

    @ApiOperation("增加一部电影")
    @RequestMapping(value="/addfilm",method = RequestMethod.POST)
    @ResponseBody
    public Film addfilm(@RequestBody Film film){
       Film film1 = filmService.addFilm(film);
       return  film1;
    }


    @ApiOperation("删除一部电影")
    @RequestMapping(value = "/deletefilms")
    @ResponseBody
    public MSG delete(@RequestParam("id")String id){
        filmService.deletefilm(id);
        return new MSG("200");
    }







    //前端ajax get重新加载showfilm.html
    @ApiOperation("前端ajax get重新加载showfilm.html")
    @RequestMapping(value="/showfilms" ,method = RequestMethod.GET)
    String get(){
        return "/html/showfilm.html";
    }




    //获取所有的豆瓣热门电影信息
    @ApiOperation("获取所有豆瓣豆瓣热门电影")
    @RequestMapping(value="/getfilms")
    @ResponseBody
    FilmGrid  getfilms(@RequestParam("current") int current,@RequestParam("rowCount") int rowCount){
        /*List list = filmService.getFilms();*/
        int total=filmService.getfilmnum();
        List list1 = filmService.getpageFilms(current,rowCount);
        FilmGrid grid = new FilmGrid();
        grid.setCurrent(current);
        grid.setRowCount(rowCount);
        grid.setRows(list1);
        grid.setTotal(total);
        return grid;
    }

    //获取根据id获取一部电影的信息  编辑电影用到
    @ApiOperation("根据id获取一部电影的信息")
    @RequestMapping(value = "/getfilmbyid")
    @ResponseBody
    public MSG getfilmbyid(@RequestParam("id") String id){
        Film film=filmService.getfilmbyid(id);
        return new MSG("200",film);
    }




    //根据条件查询电影
    @ApiOperation("测试根据条件查询电影")
    @RequestMapping(value="/getCondition")
    @ResponseBody
    FilmGrid getfilmbycondition(@RequestParam("current") int current,@RequestParam("rowCount") int rowCount,
                                @RequestParam("condition") String  condition,
                                @RequestParam("keyword") String keyword){
        System.out.println("condition"+condition+"keyword"+keyword);
        List list =filmService.getFilmsByCondition(condition,keyword);
        FilmGrid grid = new FilmGrid();
        grid.setTotal(list.size());
        grid.setRows(list);
        grid.setRowCount(current);
        grid.setCurrent(rowCount);
        return grid;
    }


    @ApiOperation("获取所有演员列表XML")
    @RequestMapping(value="/listFilmsXml",produces={"application/xml;charset=UTF-8"},method = RequestMethod.GET)
    @ResponseBody
    public FilmGrid listFilmsXml(@RequestParam("current") int current, @RequestParam("rowCount")int rowCount){
        int total=filmService.getfilmnum();
        List<Film>list =filmService.getpageFilms(current,rowCount);
        FilmGrid grid = new FilmGrid();
        grid.setCurrent(current);
        grid.setRowCount(rowCount);
        grid.setRows(list);
        grid.setTotal(total);
        return grid;
    }

/*    @ApiOperation("获取所有电影数据XML")
    @RequestMapping(value="/listFilmsXml", produces = {"application/xml;charset=UTF-8"},method = RequestMethod.GET)
    @ResponseBody
    public  FilmGrid listFIlmsXml(@RequestParam("current")int current,@RequestParam("rowCount") int rowCount){
        int total = filmService.getfilmnum();
        List<Film> films = filmService.getpageFilms(current,rowCount);
        FilmGrid grid = new FilmGrid();
        grid.setCurrent(current);
        grid.setRowCount(rowCount);
        grid.setRows(films);
        grid.setTotal(total);
        return  grid;
    }*/


    @ApiOperation("把电影信息导出为Excel")
    @RequestMapping(value = "/exportfilms",method =RequestMethod.GET)
    public void export(HttpServletResponse response)throws Exception{
        InputStream is =filmService.getInputStream();
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("contentDisposition","attachment;filename=AllUser.xls");
        ServletOutputStream outputStream=response.getOutputStream();
        IOUtils.copy(is,outputStream);
    }



    @ApiOperation("删除一则电影评论")
    @RequestMapping(value = "/deletefilmcontextbyid")
    @ResponseBody
    public MSG deletefilmcontet_byid(@RequestParam("id")String  id ){
        filmService.deletefilmcontet_byid(id);
        return new MSG("200");
    }



    @ApiOperation("跳转到showfilmContext.html")
    @RequestMapping(value = "/showfilmcontext")
    String get3(@RequestParam("id") String id){
        String str="/html/showfilmcontext.html?filmid=%s";
        String url= String.format(str, id);
        return url;
    }


    //前端 ajax post重新加载showfilm.html
    @ApiOperation("前端 ajax post 重新加载showfilm.html")
    @RequestMapping(value="/showfilms" ,method = RequestMethod.POST)
    String get2(){
        return "/html/showfilm.html";
    }


    @ApiOperation("查询指点电影的影评")
    @RequestMapping(value = "/getfilmContex")
    public  String getfilmContext(){
        return "/html/showfilmcontext.html";

    }



    /*获取指定电影的影评信息*/
    @ApiOperation("获取指定电影的影评信息")
   @RequestMapping(value = "/getFilmContext")
   @ResponseBody
    filmContextGrid getFilmContext(@RequestParam("id") String id,
                                   @RequestParam("current") int current,
                                   @RequestParam("rowCount") int rowCount){
        filmContextGrid grid = new filmContextGrid();
        /*List list = filmService.getFilmContext(id);*/  //获取所有电影
        int total = filmService.getFilmContextnum(id);  //
        List list1 = filmService.getPagefilmContext(current,rowCount,id);
        grid.setCurrent(current);
        grid.setRowCount(rowCount);
        grid.setRows(list1);
        grid.setTotal(total);
        return grid;
   }



    @ApiOperation("获取指定影评id的影评信息  ")
    @RequestMapping(value="/getfilmcontextbyid/{id}",method = RequestMethod.GET)
    @ResponseBody
    MSG getfilmContexbyid(@PathVariable("id")int id){
        filmContext context=filmService.getContextbyid(id);
        return new MSG("200",context);
    }

    /*点击编辑电影模态框中的保存按钮执行*/
    @ApiOperation("点击编辑电影模态框中的保存按钮执行")
    @RequestMapping(value = "/updatefilmbyid")
    @ResponseBody
    public MSG updatefilminfobyid(@RequestBody Film film){
        filmService.updatefilminfobyid(film);
        return new MSG("200");
    }




   @ApiOperation("把影评信息带出为excel")
   @RequestMapping(value = "/exportfilmcontext",method = RequestMethod.GET)
   public void exportfilmcontext(HttpServletResponse response,@RequestParam("filmid") String filmid) throws Exception{
        InputStream is =filmService.getFilmContentInputStream(filmid);
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("contentDisposotion","attachment;filmname=AllUser.xls");
        ServletOutputStream outputStream=response.getOutputStream();
        IOUtils.copy(is,outputStream);
   }




   @ApiOperation("点赞")
    @RequestMapping(value = "/filmcontexlike")
    @ResponseBody
    public MSG filmcontextlike(@RequestParam("id") String filmcontext_id){
        filmService.filmcontextlike_add(filmcontext_id);
        return new MSG("200");
   }

   /*添加一则电影的影评*/
    @ApiOperation("添加一则电影的影评")
    @RequestMapping(value = "/addfilmcontext")
    @ResponseBody
    public MSG addFilmContext(@RequestParam("imbdurl")String imdburl,
                              @RequestParam("username")String username,
                              @RequestParam("likes")String likes,
                              @RequestParam("filmcontext")String filmcontext){
        filmContext context=new filmContext();
        context.setUsername(username);
        context.setLikenum(likes);
        context.setContext(filmcontext);
        context.setImbdurl(imdburl);
        filmService.addFilmContext(context);//mapper使用了主键回填
        System.out.println("dsafsdaf");
        return new MSG("200",context);
    }


}
